<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i>&nbsp;编辑推荐企业</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div style="max-width:1000px;padding:10px 0 30px 0px">
                <el-alert
                    title="建议上传的图片压缩之后再上传，便于APP请求图片资源加载速度快"
                    type="warning"
                    show-icon>
                </el-alert>
            </div>
            <el-form ref="form" :model="form" label-width="160px">
                <el-form-item label="企业名称：">
                        <el-input v-model="form.companyName" placeholder="请输入企业名称：" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="分类：">
                    <el-radio v-model="form.category" label="1" border >印刷企业</el-radio>
                    <el-radio v-model="form.category" label="2" border>包装企业</el-radio>
                    <el-radio v-model="form.category" label="3" border>数码快印</el-radio>
                    <el-radio v-model="form.category" label="4" border>设备耗材</el-radio>
                    <div style="color:red;">*每个分类最多增加30条数据，请先确认该分类下是否已经达到限额！</div>
                </el-form-item>
                <el-form-item label="地址：">
                    <el-input v-model="form.address" placeholder="请输入地址" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="联系电话：">
                    <el-input v-model="form.telephone" placeholder="请输入联系电话" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="经度：">
                    <el-input v-model="form.longitude" placeholder="请输入经度" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="纬度：">
                    <el-input v-model="form.latitude" placeholder="请输入纬度" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item>
                    <div class="Hint">提示：高德地图坐标器（经纬度的标点符号是分割符，标点符号之前是经度，标点符号之后是纬度），请复制对应经纬度数字，
                        戳这里 <a href="https://lbs.amap.com/console/show/picker" target="view_frame"> ‘高德地图坐标器’ </a> 跳转查看
                    </div>
                    <p class="example" @click="changeMapEx">点击查看示例图片>></p>
                </el-form-item>
                <el-form-item label="企业简介：">
                    <el-input type="textarea" v-model="form.summary" placeholder="请输入企业简介" autosize style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="封面图：">
                    <div class="sola-upload clearfix">
                        <div class="sola-left clearfix" v-show="form.headImg != ''">
                            <div class="sola-flex">
                                <img  :src="form.headImg" />
                                <span class="delete" @click="delPicture()"><i class="el-alert__icon el-icon-error"></i></span>
                            </div>
                        </div>
                        <div class="sola-file-box">
                            <el-button type="primary" plain>上传图片<i class="el-icon-upload el-icon--right"></i></el-button>
                            <input class="btn-file" type="file" @change="uploadBanner" v-loading.fullscreen.lock="fullscreenLoading"/>
                            <!-- <span style="color:red;padding-left:10px;">(比例规范 宽大于750)建议上传的轮播图比例是1：1 示例750X750 </span> -->
                        </div>
                    </div>
                </el-form-item>
                <el-form-item label="资质证明：">
                    <div class="sola-upload clearfix">
                        <div class="sola-left clearfix" v-show="aptitudeImg.length > 0">
                            <div v-for="(item, index) in aptitudeImg" class="sola-flex" :key="index">
                                <img  :src="item" />
                                <span class="delete" @click="deleteRow(index,item,1)"><i class="el-alert__icon el-icon-error"></i></span>
                            </div>
                        </div>
                        <div class="sola-file-box">
                            <el-button type="primary" plain>上传图片<i class="el-icon-upload el-icon--right"></i></el-button>
                            <input class="btn-file" type="file" accept="image/*" @change="uploadDetail1" v-loading.fullscreen.lock="fullscreenLoading"/>
                            <span style="color:red;padding-left:10px;">提示：最多上传 3 张图片</span>
                        </div>
                    </div>
                </el-form-item>
                <el-form-item label="业务详情图：">
                    <div class="sola-upload clearfix">
                        <div class="sola-left clearfix" v-show="businessImg.length > 0">
                            <div v-for="(item, index) in businessImg" class="sola-flex" :key="index">
                                <img  :src="item" />
                                <span class="delete" @click="deleteRow(index,item,2)"><i class="el-alert__icon el-icon-error"></i></span>
                            </div>
                            
                        </div>
                        <div class="sola-file-box">
                            <el-button type="primary" plain>上传图片<i class="el-icon-upload el-icon--right"></i></el-button>
                            <input class="btn-file" type="file" accept="image/*" @change="uploadDetail2" v-loading.fullscreen.lock="fullscreenLoading"/>
                            <span style="color:red;padding-left:10px;">提示：最多上传 9 张图片</span>
                        </div>
                    </div>
                </el-form-item>


                <el-form-item>
                    <el-button type="primary" @click="changeRelease">确认修改</el-button>
                    <el-button type="primary" plain  @click="returnList">返回列表</el-button>
                    
                </el-form-item>
            </el-form>
        </div>
        <!-- 图片示例弹框 -->
        <el-dialog title="图片示例" :visible.sync="isMap" width="680px" center>
            <div class="del-dialog-cnt"><img class="mapImage" src="https://img.dddiancan.com/EOK9RH9PY0WW%29MYPW%5DI58$D.png" alt=""></div>
            <el-button type="primary" @click="isMap = false">关闭</el-button>
        </el-dialog>
    </div>
</template>
<script>
import {postUploadBase64} from '@/api/public'
import {getYinCEdit,getYinCDetail} from '@/api/company'

export default {
    name:"YinCAdd",
    data() {
        return {
            id:0,//印刷厂id
            form:{
                companyName:'',//企业名称
                address:'',//企业地址
                longitude:'',//经度
                latitude:'',//纬度
                category:'0',//分类：1印刷企业、2包装企业、3数码快印、4设备耗材
                telephone:'',//联系电话
                summary:'',//企业简介
                headImg:'',//企业logo
            },//新增
            isMap:false,//是否打开图片示例
            fullscreenLoading:false,//图片上传中
            accept: 'image/gif, image/jpeg, image/png, image/jpg', //图片上传类型
            aptitudeImg:[],//资质证明图片数组,没有传[]
            businessImg:[],//业务类图片数组，没有传[]
        }
    },
    //实例创建
    created(){
        if(this.$route.query.id){
            this.id = this.$route.query.id;
            this.getDetail();//获取印厂详情
        }
    },
    //计算属性
    computed:{
   
    },
    watch:{
        
    },
    //方法
    methods: {
        //确认新增
        changeRelease(){
            // var RegExp = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
            if(this.form.companyName == ''){
                this.$notify.info({title: '提示',message: '请输入印刷厂名称'}); return false;
            // }else if(this.form.category == ''){
            //     this.$notify.info({title: '提示',message: '请选择分类'}); return false;
            // }else if(this.form.address == ""){
            //     this.$notify.info({title: '提示',message: '请输入印刷厂地址'}); return false;
            // }else if(this.form.telephone == ""){
            //     this.$notify.info({title: '提示',message: '请输入联系电话'}); return false;
            // }else if(RegExp.test(this.form.telephone) == false){
            //     this.$notify.info({title: '提示',message: '号码格式不正确或者位数不正确'}); return false;
            // }else if(this.form.longitude == ""){
            //     this.$notify.info({title: '提示',message: '请输入经度'}); return false;
            // }else if(this.form.latitude == ""){
            //     this.$notify.info({title: '提示',message: '请输入纬度'}); return false;
            // }else if(this.form.summary == ""){
            //     this.$notify.info({title: '提示',message: '请输入简介'}); return false;
            // }else if(this.form.logo == ''){
            //     this.$notify.info({title: '提示',message: '请上传封面图'}); return false;
            // }else if(this.aptitudeImg.length < 1){
            //     this.$notify.info({title: '提示',message: '请上传资质证明'}); return false;
            // }else if(this.businessImg.length < 1){
            //     this.$notify.info({title: '提示',message: '请上传业务类图片'}); return false;
            }else{
                let _this = this;
                _this.form.aptitudeImg = JSON.stringify(this.aptitudeImg);
                _this.form.businessImg = JSON.stringify(this.businessImg);
                if(_this.form.longitude == ''){
                    _this.form.longitude = 0;
                }
                if(_this.form.latitude == ''){
                    _this.form.latitude = 0;
                }
                console.log(_this.form,'form')
                getYinCEdit(_this.form).then(res => {
                    if(res.code == 200){
                        _this.$message({
                            message: '修改成功！',
                            type: 'success'
                        });
                        setTimeout(function(){
                            _this.$router.push('/platform/list');
                        },1000);
                    }else {
                        _this.$notify.error({
                            title: '修改失败',
                            duration:2000,
                            message: '失败原因：'+ res.msg
                        });
                    }
                })
            }
        },
        //获取详情
        getDetail(){
            getYinCDetail(this.id).then(res => {
                if(res.code == 200){
                    this.form = res.data;
                    this.form.category = res.data.category.toString();
                    this.form.headImg = res.data.head_img;
                    this.form.companyName = res.data.name;
                    this.aptitudeImg = JSON.parse(res.data.aptitude_img);
                    this.businessImg = JSON.parse(res.data.business_img);
                }else {
                    this.$message({
                        type: 'error',
                        message: res.msg
                    }); 
                }
            })
        },
        //上传封面图
        uploadBanner(event){
            this.uploadImages(event,1)
        },
        //上传资质证明
        uploadDetail1(event){
            if(this.aptitudeImg.length == 3){
                this.$message({
                    message: '只能上传3张图片',
                    type: 'warning'
                }); 
            }else {
                this.uploadImages(event,2);
            }
        },
        //上传业务类图片
        uploadDetail2(event){
            if(this.businessImg.length == 3){
                this.$message({
                    message: '只能上传9张图片',
                    type: 'warning'
                }); 
            }else {
                this.uploadImages(event,3);
            }
            
        },
        //删除图片列表中某一项
        deleteRow(index,item,state){
            if(state == 1){
                //资质证明
                this.aptitudeImg.splice(index,1);
            }else if(state == 2){
                //业务类图片
                this.businessImg.splice(index,1);
            }
            this.$message.success('删除成功');
        },
        //删除封面图
        delPicture(){
            this.form.headImg = '';
        },
        //上传企业logo图 file
        uploadImages(event,imgType){
            var _event = event;
            var _this = this;
            let reader =new FileReader();  
            let img1 = event.target.files[0];  
            let type = img1.type;//文件的类型，判断是否是图片  
            let size = img1.size;//文件的大小，判断图片的大小
            const files = event.target;  
            if(this.accept.indexOf(type) == -1){  
                this.$message({
                    message: '请选择gif,jpeg,png,jpg格式图片上传',
                    type: 'warning'
                });
                return false;  
            }  
            else if(size>3145728){  
                this.$message({
                    message: '请选择3M以内的图片上传',
                    type: 'warning'
                }); 
                return false;  
            }else if (typeof(FileReader) === 'undefined') {
                this.$notify.error({
                    title: '错误',
                    message: '抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！'
                });
            }else {
                this.fullscreenLoading = true;
                let reader = new FileReader();
                reader.readAsDataURL(img1);
                reader.onload = function(e) {
                    var base = this.result;
                    postUploadBase64(base).then(response=>{
                        // console.log(response);
                        if(response.code==200){
                            if(imgType == 1){
                                //封面图
                                _this.form.headImg = response.data;
                            }else if(imgType == 2){
                                //资质证明
                                let aptitudeImgList = _this.aptitudeImg;
                                _this.aptitudeImg = aptitudeImgList.concat(response.data);
                            }else if(imgType == 3){
                                let businessImgList = _this.businessImg;
                                _this.businessImg = businessImgList.concat(response.data);
                            }
                            _this.$message({
                                message: '恭喜你，图片上传成功',
                                type: 'success'
                            });
                        }else{
                            _this.$notify.error({
                                title: '上传图片失败',
                                duration:2000,
                                message: '失败原因：'+response.msg
                            });
                        }
                        setTimeout(function(){
                            _this.fullscreenLoading = false;
                        },300);
                    })
                }
                
            }
        },
        //返回列表页
        returnList(){
            this.$router.push('/platform/list');
        },
        //打开图片示例
        changeMapEx(){
            this.isMap = true;
        }
    }
}
</script>
<style lang="stylus" scoped>
    .el-form-item .el-cascader{
        width :400px;
    }
    .el-cascader-menus .el-cascader-menu{
        width :240px
    }
    .sola-upload{
        .sola-left{
            width :100%;
            height :auto;
            .sola-flex{
                float:left;
                width: 360px;
                height: 180px;
                margin-bottom: 20px;
                margin-right:15px;
                position:relative;
                .delete{
                    position:absolute;
                    top:2px;
                    right:13px;
                    color: #f56c6c;
                    display:inline-block;
                    padding:5px 5px;
                    .el-alert__icon{font-size:28px;cursor:pointer}
                }
                img {
                    width:100%;
                    height:100%;
                }
            }
        }
        .sola-file-box{
            
            position:relative;
            .btn-file{
                position:absolute;
                top:0;
                left:0;
                z-index: 1;
                opacity:0;
                width :120px;
                height :40px;
                outline: 0;
                background-color :none;
                cursor:pointer;
            }
        }

    }
    .Hint {
        width:600px;
        font-size :14px;
        a {
            color:red;
        }
    }
    .example {
        font-size:14px;
        color:#409EFF
    }
    .mapImage {
        width:620px;
        height:600px;
    }
</style>


